#!/usr/bin/perl

my $sentence = [];
my $parents = {};
my $normal = "";
my $waiting = "";
my $i = 0;
while (my $line = <>) {
	# Reset sentence
	if ($line =~ /^\s*$/) {
		# Create 
		if (@$sentence) {
			# Find "WAIT" analysis
			for (my $i = 0; $i < scalar(@$sentence); ++$i) {
				my $args = $sentence->[$i];			
				if ($args->[6] >= $i+1) {
					$args->[6] = tland($parents, $i+1, $i+1);
					$args->[7] = "WAIT";
				}
				$waiting .= join("\t", @$args);
			}

			# Output one copy of WAIT and two copies of NORMAL
			print $waiting, "\n";
			print $normal, "\n";
			print $normal, "\n";
		}

		# Reset variables
		$i = 0;
		$sentence = [];
		$normal = "";
		$waiting = "";
		$parents = {};
	} else {
		++$i;
		my @x = split(/\t/, $line);
		my $args = [@x];
		$parents->{$i} = $args->[6];
		push @$sentence, $args;
		$normal .= join("\t", @$args);
	}
}

sub tland {
	my $parents = shift;
	my $tparent = shift;
	my $node = shift;
	if (! defined($tparent)) {
		return 0;
	} elsif ($tparent < $node) {
		return $tparent;
	} else {
		return tland($parents, $parents->{$tparent}, $node);
	}
}
